home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 23.zip
/
BS1 part 23
/
C-light d2.adf
/
Random.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-08-08
|
1KB
|
49 lines
/*------------------------- r a n d o m . c -------------------------*/
/* This routine generates random numbers that are greater than or */
/* equal to zero and less than one. If called with a non-zero value */
/* random(1777) the value is used as a new seed. If called with a */
/* value of zero, the random number is generated using the old seed. */
/* If no old seed exists, then 177777 is used as a new seed. */
/*-------------------------------------------------------------------*/
#include "stdio.h"
#include "math.h"
float random(setseed)
int setseed;
{
static int a = 8195;
static int num;
float value;
static int oldseed = 0;
static int TWO_to_27 = 134217728; /* 134217728 = 2**27 */
if(setseed == 0)
{
if(oldseed == 0) /* Generate own seed. */
{
oldseed = 177777;
num = (a * oldseed)%TWO_to_27;
oldseed = num;
value = ((float)num)/((float)TWO_to_27);
if(value < 0.)value = (-value);
return(value);
} else /* Use old seed. */
{
num = (a * oldseed)%TWO_to_27;
oldseed = num;
value = ((float)num)/((float)TWO_to_27);
if(value < 0.)value = (-value);
return(value);
}
} else /* set seed if provided with one. */
{
oldseed = setseed;
num = (a * oldseed)%TWO_to_27;
oldseed = num;
value = ((float)num)/((float)TWO_to_27);
if(value < 0.)value = (-value);
return(value);
}
}